﻿using System;
using System.Windows.Forms;
using Library;

namespace GameTool
{
    public partial class MatrixSubForm : Form
    {
        #region Properties
        MeCung_Form1 parent;
        public int[] Matrix;

        public int IndexForcus;
        #endregion

        #region Constructos
        public MatrixSubForm()
        {
            InitializeComponent();
        }

        public MatrixSubForm(MeCung_Form1 parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        #endregion

        #region Methos
        public void InitMatrix(int numbers) 
        {
            try
            {
                Matrix = new int[numbers];
                ResetMatrix();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        public void ResetMatrix()
        {
            try
            {
                for (int i = 0; i < Matrix.Length; i++)
                {
                    Matrix[i] = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        public void InputDataToMatrix(int index, int value)
        {
            try
            {
                Matrix[index] = value;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        public void ShowForm(int index)
        {
            IndexForcus = index;
            this.Show();
        }

        public void UpdateMatrix(string value)
        {
            try
            {
                string[] str = value.Split(' ');
                Matrix = new int[str.Length];
                for (int i = 0; i < str.Length; i++)
                {
                    Matrix[i] = int.Parse(str[i]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        public bool CheckValidate()
        {
            int counterBegin = 0;
            int counterTarget = 0;
            try
            {
                for (int i = 0; i < Matrix.Length; i++)
                {
                    if (Matrix[i] == -2)
                        counterBegin++;
                    if (Matrix[i] == -3)
                        counterTarget++;
                }
                if (counterBegin == 1 && counterTarget == 1)
                {
                    return true;
                }
                else
                {
                    return false;                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
            return false;
        }

        #endregion

        #region Events
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // Gia tri 0 la di duoc khong co cau hoi.
                if (radioButton1.Checked)
                {
                    // Tuong -1
                    parent.InputDataToMatrix(1, IndexForcus);
                    Matrix[IndexForcus] = -1;
                }
                else
                {
                    if (radioButton2.Checked)
                    {
                        // Diem xuat phat -2
                        parent.InputDataToMatrix(2, IndexForcus);
                        Matrix[IndexForcus] = -2;
                    }
                    else
                    {
                        if (radioButton3.Checked)
                        {
                            // Diem ket thuc -3
                            parent.InputDataToMatrix(3, IndexForcus);
                            Matrix[IndexForcus] = -3;
                        }
                        else
                        {
                            if (radioButton4.Checked)
                            {
                                // Co cau hoi muc 1
                                parent.InputDataToMatrix(4, IndexForcus);
                                Matrix[IndexForcus] = 1;
                            }
                            else
                            {
                                if (radioButton5.Checked)
                                {
                                    // Co cau hoi muc 2
                                    parent.InputDataToMatrix(5, IndexForcus);
                                    Matrix[IndexForcus] = 2;
                                }
                                else
                                {
                                    if (radioButton6.Checked)
                                    {
                                        // Co cau hoi muc 3
                                        parent.InputDataToMatrix(6, IndexForcus);
                                        Matrix[IndexForcus] = 3;
                                    }
                                    else
                                    {
                                        if (radioButton7.Checked)
                                        {
                                            // Co cau hoi muc 3
                                            parent.InputDataToMatrix(7, IndexForcus);
                                            Matrix[IndexForcus] = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                this.Hide();
                this.parent.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.Hide();
                this.parent.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }
        #endregion
    }
}
